package swing;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

//import swing.ScheduleGUI.ObserveJobUpdateGUI;

import main.ThreadPoolJob;

public class AddTrayIcon {
	private JFrame frame;
	private MenuItem exitItem;
	private MenuItem collectItem;
	private MenuItem pauseItem;
	private MenuItem cancelItem;
	
	public AddTrayIcon(JFrame f){
		this.frame = f;
		
		frame.addWindowListener(new WindowAdapter(){
        	public void windowIconified(WindowEvent e){
        		if(frame.isVisible())
        			frame.setVisible(false);
        	}
        });
		
		final TrayIcon trayIcon;
		if(SystemTray.isSupported()){
			 SystemTray tray = SystemTray.getSystemTray();
			 Image image = Toolkit.getDefaultToolkit().getImage("");
			 MouseListener mouseListener = new MouseListener(){
				@Override
				public void mouseClicked(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void mousePressed(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void mouseReleased(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}       				 
			 };
			 
			 ActionListener exitListener = new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					System.exit(0);
				}
			 };
			 exitItem = new MenuItem("Exit");
			 exitItem.addActionListener(exitListener);
			 
			 ActionListener collectListener = new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					
				}
			 };
			 collectItem = new MenuItem("Collect books now");
			 collectItem.addActionListener(collectListener);
			 collectItem.setEnabled(true);
			 
			 ActionListener pauseListener = new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						
					}
			 };
			 pauseItem = new MenuItem("Pause");
			 pauseItem.addActionListener(pauseListener);
			 pauseItem.setEnabled(false);
			 
			 ActionListener cancelListener = new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						
					}
			 };
			 cancelItem = new MenuItem("Cancel");
			 cancelItem.addActionListener(cancelListener);
			 cancelItem.setEnabled(false);
			 
			 PopupMenu popup = new PopupMenu();
			 popup.add(collectItem);
			 popup.add(pauseItem);
			 popup.add(cancelItem);
			 popup.add(exitItem);
			 
			 trayIcon = new TrayIcon(image, "Tray Demo", popup);
			 
			 ActionListener actionListener = new ActionListener() {
				 public void actionPerformed(ActionEvent e) {
					 //trayIcon.displayMessage("Action Event", 
			         //"An Action Event Has Been Performed!",
			         //TrayIcon.MessageType.INFO);
					 if(!frame.isVisible())
							frame.setVisible(true);
			     }
			 };
			 trayIcon.setImageAutoSize(true);
			 trayIcon.addActionListener(actionListener);
			 trayIcon.addMouseListener(mouseListener);
			 
			 try {
			     tray.add(trayIcon);
			 } catch (AWTException e1) {
				 System.out.println("TrayIcon could not be added.");
			 }
		}
		//ObserveJobUpdateGUI r = new ObserveJobUpdateGUI();
		//Thread t = new Thread(r);
		//t.start();
	}
	/*public class ObserveJobUpdateGUI implements Runnable{
		private boolean updateGuiSth1 = false;
		private boolean updateGuiSth2 = false;
		@Override
		public void run() {
			// TODO Auto-generated method stub
			//ss.getDefaultScheduler()
			while(true){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if(ThreadPoolJob.getIsJobRunning2()){
					if(!updateGuiSth1){
						System.out.println("tray");
						if(collectItem.isEnabled())
							collectItem.setEnabled(false);
						if(!pauseItem.isEnabled())
							pauseItem.setEnabled(true);
						if(!cancelItem.isEnabled())
							cancelItem.setEnabled(true);
						updateGuiSth1 = true;
						updateGuiSth2 = false;
					}
				}else{
					//System.out.println("tray");
					if(!updateGuiSth2){
						if(!collectItem.isEnabled())
							collectItem.setEnabled(true);
						if(pauseItem.isEnabled())
							pauseItem.setEnabled(false);
						if(cancelItem.isEnabled())
							cancelItem.setEnabled(false);
						updateGuiSth1 = false;
						updateGuiSth2 = true;
					}
				}	
			}
		}
		
	}*/
}
